//
//  AppDelegate.m
//  shopping
//
//  Created by 马田成 on 2017/8/30.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "AppDelegate.h"
#import "MyTabBarController.h"
#import "IQKeyboardManager.h"
#import "LoginViewController.h"
//#import "AppKeFuLib.h"

#import <AppKeFuFramework/AppKeFuFramework.h>

@interface AppDelegate ()

@end

@implementation AppDelegate
#pragma mark ----------------------- islogin ---------------------

- (void)islogin{
    
    NSMutableDictionary *productParams = [[NSMutableDictionary alloc]init];
    [productParams setValue: [PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    
    [[MNetworkHandler sharedInstance]conURL:adminLoginCheckToken
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   
                                   if ([[returnData objectForKey:@"code"]intValue] == 1) {
                                       
                                       ///去掉顶部UINavigationBar的一条黑线
                                       [[UINavigationBar appearance]  setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
                                       [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
                                       [UINavigationBar appearance].translucent = NO;
                                       
                                       
                                       MyTabBarController *barController = [[MyTabBarController alloc] init];
                                       barController.view.backgroundColor = getColor(@"ffffff");
                                       self.window.rootViewController = barController;
                                       self.window.backgroundColor = [UIColor whiteColor];
                                       [self.window makeKeyAndVisible];
                                       
                                   }else{
                                       
                                       ///去掉顶部UINavigationBar的一条黑线
                                       [[UINavigationBar appearance]  setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
                                       [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
                                       [UINavigationBar appearance].translucent = NO;
                                       
                                       LoginViewController *welcomeVC = [[LoginViewController alloc] init];
                                       UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:welcomeVC];
                                       self.window.rootViewController = navC;
                                       self.window.backgroundColor = [UIColor whiteColor];
                                       [self.window makeKeyAndVisible];
                                   }
                                   
                               } failureBlock:^(NSError *error) {
                                   
                                   NSLog(@"%@",error);
//                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:1.0f];
                                   
                                   ///去掉顶部UINavigationBar的一条黑线
                                   [[UINavigationBar appearance]  setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
                                   [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
                                   [UINavigationBar appearance].translucent = NO;
                                   
                                   LoginViewController *welcomeVC = [[LoginViewController alloc] init];
                                   UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:welcomeVC];
                                   self.window.rootViewController = navC;
                                   self.window.backgroundColor = [UIColor whiteColor];
                                   [self.window makeKeyAndVisible];
                                   
                               }];
}

//}
//

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    manager.enable = YES;
    manager.shouldResignOnTouchOutside = YES;//这个是点击空白区域键盘收缩的开关
    manager.enableAutoToolbar = NO;//这个是它自带键盘工具条开关
   
    
    
//    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//    // Override point for customization after application launch.
//    
//    [self.window makeKeyAndVisible];
    //步骤一：初始化操作
     [[AppKeFuLib sharedInstance] enableIPServerMode:FALSE];//务必在调用login接口之前调用
    [[AppKeFuLib sharedInstance] loginWithAppkey:@"3b136e7d053df4cf077c56831d15a875"];
    
    //注册离线消息推送
    //xcode 6
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        // iOS 8 Notifications
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
        
        [application registerForRemoteNotifications];
    } else {
        // iOS < 8 Notifications
        [application registerForRemoteNotificationTypes:
         (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
    }
    //xcode 5
#else
    // iOS < 8 Notifications
    [application registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
#endif

    
    [self islogin];
//    LoginViewController *vc = [[LoginViewController alloc] init];
//    UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:vc];
//    self.window.rootViewController = nav2;
//    
//    
//    self.window.backgroundColor = [UIColor whiteColor];
//    
//    [self.window makeKeyAndVisible];
    
    
//    UINavigationBar *bar = [UINavigationBar appearance];
//    
//    bar.barStyle=UIBarStyleBlackTranslucent;
//    
//    //设置显示的颜色
//    bar.barTintColor =[ZJCModel colorWithString:@"ffffff"];
//    
//    //设置字体颜色
//    bar.tintColor = [UIColor whiteColor];
//    
//    [bar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
//    
//    // TAB BAR
//    self.tabBarController = [[MyTabBarController alloc] init];
//    self.window.rootViewController = self.tabBarController;
//    self.window.backgroundColor = [UIColor whiteColor];
//    
//    self.window.backgroundColor = [UIColor whiteColor];
//    
//    [self.window makeKeyAndVisible];

    //123412341234
    // Override point for customization after application launch.
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    
    
    [[AppKeFuLib sharedInstance] enableIPServerMode:FALSE];//务必在调用login接口之前调用
    [[AppKeFuLib sharedInstance] loginWithAppkey:@"3b136e7d053df4cf077c56831d15a875"];
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    
    
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
#pragma mark 离线消息推送
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    //同步deviceToken便于离线消息推送, 同时必须在管理后台上传 .pem文件才能生效
    [[AppKeFuLib sharedInstance] uploadDeviceToken:deviceToken];
    
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    NSLog(@"收到推送消息。%@", userInfo);
    /*
     {
     aps =     {
     alert = "客服消息:555";
     badge = 1;
     sound = default;
     };
     channel = appkefu;
     message = 555;
     workgroup = wgdemo;
     }
     */
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    //NSLog(@"注册推送失败，原因：%@",error);
}



@end
